package com.reggie.console.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.reggie.acl.dataobject.Category;
import com.reggie.common.model.PageRequest;
import com.reggie.console.annotation.AuthRule;
import com.reggie.console.dto.ResultDTO;
import com.reggie.console.enums.ReggieRole;
import com.reggie.console.util.ResultBuilder;
import com.reggie.service.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author yth
 * @since 2023-02-04
 */
@Slf4j
@RestController
@RequestMapping("/category")
@AuthRule(roles = {ReggieRole.EMPLOYEE})
public class CategoryController {

    private static final String EXPIRE_TIME = "1860";   // 31 MIN

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     *
     * @param page     当前页码
     * @param pageSize 每页数据量
     * @return 数据
     */
    @GetMapping("/page")
    @AuthRule(roles = {ReggieRole.CUSTOMER})
    public ResultDTO<IPage<Category>> page(int page, int pageSize) {
        PageRequest request = new PageRequest();
        request.setPage(page);
        request.setPageSize(pageSize);
        IPage<Category> pageInfo = categoryService.page(request);
        return ResultBuilder.ok(pageInfo);
    }

    /**
     * 新增分类
     *
     * @param category 分类数据
     * @return 消息
     */
    @CacheEvict(value = "categoryCache", allEntries = true)
    @PostMapping
    @AuthRule(roles = {ReggieRole.EMPLOYEE})
    public ResultDTO<String> save(@RequestBody Category category) {
        log.info("save()");
        // 添加数据
        categoryService.save(category);
        return ResultBuilder.ok("添加分类成功");
    }

    /**
     * 修改分类
     *
     * @param category 分类数据
     * @return 消息
     */
    @CacheEvict(value = "categoryCache", allEntries = true)
    @PutMapping
    public ResultDTO<String> update(@RequestBody Category category) {
        log.info("update()");
        categoryService.updateById(category);
        return ResultBuilder.ok("修改分类成功");
    }

    /**
     * 删除分类
     *
     * @param id id
     * @return 消息
     */
    @CacheEvict(value = "categoryCache", allEntries = true)
    @DeleteMapping
    public ResultDTO<String> remove(long id) {
        log.info("remove()");
        categoryService.removeById(id);
        return ResultBuilder.ok("删除分类成功");
    }

    /**
     * 根据类型，获取分类集合<p>
     * type为null，则查询全部
     *
     * @param type 类型：1 = 菜品分类， 2 = 套餐分类， null = 全部
     * @return 数据
     */
    @Cacheable(value = "categoryCache#" + EXPIRE_TIME, sync = true,
            key = "#root.methodName", condition = "#type == null")
    @GetMapping("/list")
    @AuthRule(roles = {ReggieRole.CUSTOMER})
    public ResultDTO<List<Category>> list(Integer type) {
        log.info("list()");
        List<Category> categories = categoryService.listByType(type);
        return ResultBuilder.ok(categories);
    }

}

